#region Microsoft Public License
/*
 * (c) Copyright Chenjun Wu. http://utablesdk.codeplex.com
 * 
 * This source is subject to the Microsoft Public License (Ms-PL).
 * Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
 * All other rights reserved.
 */
#endregion

﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

namespace UTable.Input
{
    public class CompositeInputProvider: IInputProvider
    {
        public CompositeInputProvider()
        {
            Providers = new ObservableCollection<IInputProvider>();
            Providers.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Providers_CollectionChanged);
        }

        void Providers_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                foreach (object o in e.NewItems)
                    if (o is IInputProvider)
                        OnProviderAdded(o as IInputProvider);
            }
            
            if (e.OldItems != null)
            {
                foreach (object o in e.OldItems)
                    if (o is IInputProvider)
                        OnProviderRemoved(o as IInputProvider);
            }

            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    break;
                case NotifyCollectionChangedAction.Remove:
                    break;
                case NotifyCollectionChangedAction.Replace:
                    break;
                case NotifyCollectionChangedAction.Reset:
                    break;
            }
        }

        private void OnProviderAdded(IInputProvider provider)
        {
            provider.InputTriggered += ProviderInputTriggered;
        }

        private void OnProviderRemoved(IInputProvider provider)
        {
            provider.InputTriggered -= ProviderInputTriggered;
        }

        void ProviderInputTriggered(InputEventArgs args)
        {
            if (InputTriggered != null)
                InputTriggered(args);
        }

        public ObservableCollection<IInputProvider> Providers;

        #region IInputProvider Members

        public event InputEventHandler InputTriggered;

        public void Initialize(UTable.Objects.UApplication context)
        {}

        #endregion
    }
}
